<template>
  <AdminLayout>
    <el-table
      empty-text="暂无数据"
      :data="ArticleTable"
      style="width: 100%"
      border
    >
      <el-table-column label="#" prop="id" align="center" width="100" />
      <el-table-column label="标题" prop="title" align="center" />
      <el-table-column label="描述" prop="desc" align="center" />
      <el-table-column label="发布时间" align="center">
        <template #default="scope">
          <i class="el-icon-time"></i>
          {{ scope.row.created_at }}
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center">
        <template #default="scope">
          <el-button>{{ scope.row.author }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            size="mini"
            icon="el-icon-thumb"
            @click="handleDetails(scope.row.id)"
            circle
          >
          </el-button>
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row.id)"
            icon="el-icon-edit"
            circle
          >
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
            circle
          >
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      align="center"
      background
      layout="prev, pager, next"
      :current-page="page"
      @current-change="currentChange"
    >
    </el-pagination>

    <el-button @click="this.$router.push({ name: 'ArticleCreation' })"
      >创建新文章</el-button
    >
  </AdminLayout>
</template>

<script>
import { mapState, mapActions } from "vuex";

export default {
  name: "Article",
  computed: {
    ...mapState("articleTable", ["ArticleTable"]),
  },
  created() {
    this.GetArticleTable();
  },
  data() {
    return {
      page: 1,
    };
  },
  methods: {
    ...mapActions("articleTable", ["GetArticleTable"]),
    ...mapActions("Item", ["delArticle"]),
    currentChange(page) {
      this.GetArticleTable(page);
    },
    handleDetails(id) {
      this.$router.push({
        name: "ArticleDetails",
        params: {
          id,
        },
      });
    },
    handleEdit(id) {
      this.$router.push({
        name: "ArticleAlter",
        params: {
          id,
        },
      });
    },
    handleDelete(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          try {
            await this.delArticle(id);
            await this.GetArticleTable();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          } catch (e) {
            this.$message({
              type: "error",
              message: "删除失败",
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped lang="stylus"></style>
